package com.guragura.screens;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.guragura.Guragura;
import com.guragura.util.Constants;
import com.guragura.util.GuraScreenInputListener;
import com.guragura.util.GuraSettings;

/**
 * @author hartono
 * untuk option screen pada game ini
 */
public class SettingScreen extends GuraScreen {

	private Stage stage;
	private Button soundOnButton;
	private Button vibrationOnButton;
	private Button soundOffButton;
	private Button vibrationOffButton;

	private Button backButton;
	private Image soundImage;
	private Image vibrationImage;
	private Image background;

	public SettingScreen() {
		// TODO Auto-generated constructor stub
	}

	public SettingScreen(Guragura guragura) {
		super(guragura);
	}

	@Override
	public void initScreen() {
		// TODO Auto-generated method stub
		stage = new Stage(320, 480, true);
		inputListener = new GuraScreenInputListener(camera, stage);
	}

	@Override
	public void onScreenRunning() {
		// TODO Auto-generated method stub
		stage.draw();
	}

	@Override
	public void onScreenPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScreenLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initFromRes() {
		// TODO Auto-generated method stub
		soundOnButton = new Button(
				guragura.atlasInterface.findRegion(Constants.SOUND_ON_BUTTON));
		vibrationOnButton = new Button(
				guragura.atlasInterface.findRegion(Constants.VIBRATE_ON_BUTTON));

		soundOffButton = new Button(
				guragura.atlasInterface.findRegion(Constants.SOUND_OFF_BUTTON));
		vibrationOffButton = new Button(
				guragura.atlasInterface
						.findRegion(Constants.VIBRATE_OFF_BUTTON));

		backButton = new Button(guragura.atlasInterface.findRegion(
				Constants.BACK_BUTTON, 1), guragura.atlasInterface.findRegion(
				Constants.BACK_BUTTON, 2));

		background= new Image(
				guragura.atlasInterface.findRegion(Constants.OPTION_SCREEN));
		vibrationImage = new Image(
				guragura.atlasInterface.findRegion(Constants.VIBRATION_TEXT));
		soundImage = new Image(
				guragura.atlasInterface.findRegion(Constants.SOUND_TEXT));

		soundOnButton.x = 320- soundOnButton.width - 100;

		vibrationOnButton.x = soundOnButton.x;
		backButton.x = 320- backButton.width - 10;
		backButton.y = 20;

		soundOnButton.y = (480- soundOnButton.height * 2 - vibrationImage.height +10) / 2;
		vibrationOnButton.y = soundOnButton.y + soundOnButton.height * 2 + 10;

		soundImage.x = soundOnButton.x;
		soundImage.y = soundOnButton.y + soundOnButton.height - 10 ;

		vibrationImage.x = vibrationOnButton.x;
		vibrationImage.y = vibrationOnButton.y + vibrationOnButton.height - 15;

		soundOnButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				toggleSound();
			}

		});
		vibrationOnButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				toggleVibration();
			}

		});

		soundOffButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				toggleSound();
			}

		});
		vibrationOffButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub

				toggleVibration();
			}

		});

		backButton.setClickListener(new ClickListener() {

			@Override
			public void click(Actor arg0, float arg1, float arg2) {
				// TODO Auto-generated method stub
				guragura.popScreen();
			}
		});

		soundOffButton.x = soundOnButton.x;
		vibrationOffButton.x = vibrationOnButton.x;
		soundOffButton.y = soundOnButton.y;
		vibrationOffButton.y = vibrationOnButton.y;

		stage.addActor(background);
		
		if (GuraSettings.vibration) {
			stage.addActor(vibrationOnButton);
		} else {
			stage.addActor(vibrationOffButton);
		}

		if (GuraSettings.sound) {
			stage.addActor(soundOnButton);
		} else {
			stage.addActor(soundOffButton);
		}

		
		stage.addActor(vibrationImage);
		stage.addActor(soundImage);
		stage.addActor(backButton);

		state = SCREEN_RUNNING;
	}

	/**
	 *  melakukan toggle sound
	 */
	private void toggleSound() {
		// TODO Auto-generated method stub
		if (GuraSettings.sound) {
			stage.removeActor(soundOnButton);
			stage.addActor(soundOffButton);
			GuraSettings.sound = false;
		} else {
			stage.removeActor(soundOffButton);
			stage.addActor(soundOnButton);
			GuraSettings.sound = true;
		}
		GuraSettings.saveSettings();
		guragura.music.toggleMusic();
	}

	/**
	 *  melakukan toggle vibration
	 */
	private void toggleVibration() {
		// TODO Auto-generated method stub
		if (GuraSettings.vibration) {
			GuraSettings.vibration = false;
			stage.removeActor(vibrationOnButton);
			stage.addActor(vibrationOffButton);
		} else {
			GuraSettings.vibration = true;
			stage.removeActor(vibrationOffButton);
			stage.addActor(vibrationOnButton);
		}
		GuraSettings.saveSettings();
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		guragura.gameState = Guragura.SETTING_SCREEN;
		guragura.music.play();
	}
	
	
}
